package com.lxy.user.service;


import cn.hutool.core.lang.UUID;
import com.aliyun.avatar20220130.Client;
import com.aliyun.avatar20220130.models.*;
import com.aliyun.oss.ServiceException;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class DigitalHumanService {

    private final Client avatarClient;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    public DigitalHumanService(@Qualifier("avatarClient") Client avatarClient) {
        this.avatarClient = avatarClient;
    }


    /**
     * 启动RTC数字人实例
     * @param userId 用户ID
     * @param userName 用户名
     * @return 会话ID
     */
    public String startRtcInstance(String userId, String userName) {
        try {
            StartInstanceRequest startInstanceRequest = new StartInstanceRequest();
            startInstanceRequest.setApp(new StartInstanceRequest.StartInstanceRequestApp().setAppId("3dt85cd463"));
            startInstanceRequest.setTenantId(30620L);
            startInstanceRequest.setUser(new StartInstanceRequest.StartInstanceRequestUser()
                    .setUserId("111111")
                    .setUserName("蔡徐坤"));
            StartInstanceResponse response = avatarClient.startInstance(startInstanceRequest);
            return response.getBody().getData().getSessionId();
        } catch (Exception e) {
            log.error("启动数字人失败", e);
            throw new RuntimeException("启动数字人失败", e);
        }
    }

    /**
     * 发送文本让数字人播报
     * @param sessionId 会话ID
     * @param text 播报文本
     */
    public void sendTextMessage(String sessionId, String text) {
        try {
            sessionId=(String) redisTemplate.opsForValue().get("sessionId");
            SendMessageRequest request = new SendMessageRequest()
                    .setTenantId(30620L)
                    .setSessionId(sessionId)
                    .setTextRequest(new SendMessageRequest.SendMessageRequestTextRequest()
                            .setCommandType("START")
                            .setSpeechText(text)
                            .setId(UUID.randomUUID().toString()));
            avatarClient.sendMessage(request);
        } catch (Exception e) {
            log.error("发送文本消息失败", e);
            throw new ServiceException("数字人播报失败");
        }
    }


    /**
     * 结束数字人实例
     * @param sessionId 会话ID
     */
    public void stopInstance(String sessionId) {
        try {
            StopInstanceRequest request = new StopInstanceRequest()
                    .setTenantId(30620L)
                    .setSessionId(sessionId);

            avatarClient.stopInstance(request);
        } catch (Exception e) {
            log.error("停止数字人失败", e);
            throw new ServiceException("数字人停止失败");
        }
    }

    /**
     * 查询运行中的数字人实例
     * @param sessionId 会话ID
     * @return 实例状态信息
     */
    public QueryRunningInstanceResponse queryInstance(String sessionId) {
        try {
            QueryRunningInstanceRequest request = new QueryRunningInstanceRequest()
                    .setTenantId(30620L)
                    .setSessionId(sessionId);

            return avatarClient.queryRunningInstance(request);
        } catch (Exception e) {
            log.error("查询数字人状态失败", e);
            throw new ServiceException("数字人状态查询失败");
        }
    }
}


